package cn.muqingfeng.bbs.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.muqingfeng.api.to.Article1TO;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.util.BitmapBinder;
import cn.muqingfeng.util.ViewHolder;

import com.android.volley.toolbox.NetworkImageView;

public class Article1Adapter extends BaseAdapter {
	private Context mContext;
	private List<Article1TO> mArticles = new ArrayList<Article1TO>();

	public Article1Adapter(Context context) {
		mContext = context;
	}

	@Override
	public int getCount() {
		return mArticles.size();
	}

	@Override
	public Object getItem(int position) {
		return mArticles.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.article_item, null);
		}
		Article1TO article = mArticles.get(position);
		TextView titleView = ViewHolder.get(convertView, R.id.title);
		titleView.setText(article.getTitle());
		NetworkImageView imageView = ViewHolder.get(convertView, R.id.image);
		BitmapBinder.bindNetImage(mContext, imageView, article.getCover(), R.drawable.ic_image_default);
		TextView authorView = ViewHolder.get(convertView, R.id.author);
		authorView.setText(article.getAuthor());
		TextView timeView = ViewHolder.get(convertView, R.id.time);
//		String time = DateUtils.formatDateTime(mContext, article.getPublishTime() * 1000, DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
//				| DateUtils.FORMAT_ABBREV_ALL);
		timeView.setText(article.getPublishTime());
		TextView commentCntView = ViewHolder.get(convertView, R.id.comment_count);
		commentCntView.setText(article.getReplyCount() + "条评论");
		return convertView;
	}

	public void addNews(List<Article1TO> news) {
		if (news != null && !news.isEmpty()) {
			mArticles.addAll(news);
		}
	}

	public void clear() {
		mArticles.clear();
	}

}
